<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" session="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="renderer" content="webkit">
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
    <META HTTP-EQUIV="Expires" CONTENT="0">
    <title>预发钥匙</title>

    <link href="${pageContext.request.contextPath}/static/css/common.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/bootstrap-3.3.5/css/bootstrap.min.css"
          rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/font-awesome-4.3.0/css/font-awesome.min.css"
          rel="stylesheet">
    <!-- jqgrid-->
    <link href="${pageContext.request.contextPath}/static/plugins/jqGrid/css/custom.ui.jqgrid.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/layer/skin/layer.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/jqGrid/css/style.css" rel="stylesheet">

    <script src="${pageContext.request.contextPath}/static/js/jquery-1.11.3.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/jquery.form.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/common.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/bootstrap-3.3.5/js/bootstrap.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/jqGrid/js/i18n/grid.locale-cn.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/jqGrid/js/minified/jquery.jqGrid.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/layer/layer.js" type="text/javascript"></script>

    <style type="text/css">
        .main h5 a {
            position: absolute;
            right: 20px;
            font-size: 14px;
            bottom: 0px;
            display: inline-block;
            padding: 5px;
        }

        .table_list tbody tr {
            height: 35px;
            text-align: center;
        }
        #gbox_jqGrid{
            width: 500px;
    		float: left;
        }
        #gbox_jqGrid2{
            width: 500px;
             margin-left: 550px;
        }
        #gbox_jqGrid3{
        	width: 500px;
             margin-left: 550px;
        }
        .h1{
              margin-left: 550px;
        }
        .shuxian{
        	height=90;
        	width: 2px;
        	border-color:000000;
        	border-left-style:solid;
        	border-width:1px;
        	float: left;
        }
        .l{
		   width: 1px;
		   height: 535px;
		   background: #000;
 		   margin-left: 25px;
			 float:left;
		}
		#jqGrid_paper_left{
			width: 200px;
		}
		.left ul li{
			
			*line-height:40px;
			
			*padding-left:20px;
		}
		
		.left ul li a, .left ul li a:hover{
			*color:#fff;
		}
		.list-tab li{
			*+padding:0px 10px 10px;
		}
		.wrapper{
			*margin-left:266px;
		}
    </style>
</head>
<body view="menu-item-4142">
<jsp:include page="../../top.jsp"></jsp:include>
<div class="right">
    <div class="wrapper">
	    <div class="jqGrid_wrapper">
	            <h1>预发钥匙</h1>
	        <table id="jqGrid"></table>
	        <div class="l"></div>
	          <h1 class="h1">单元门禁</h1>
	        <table id="jqGrid2"></table>
	         <h1 class="h1">公共门禁（*勾选楼栋门时，默认配发公共门禁）</h1>
	        <table id="jqGrid3"></table>
	        <div id="jqGrid_paper"></div>
	        <input type="hidden" id="validity"/>
	        <div style="margin-top: 16px;">
	        <a>试试</a><a onclick="batch()"> 批量导入</a><span style="width: 10px">&nbsp;&nbsp;&nbsp;</span>
	        <a href="${pageContext.request.contextPath}/admin/tusers_download.action" style="margin-left: 20px;">模板下载</a>
	        <form id="uploadForm">
	        <input  id="upload" type="file" name="upload" style="display:none"/>
	        <input name="agtAttr"  id="agtAttr" type="hidden"/>
	        </form>
	        </div>
	        
	    </div>
	</div>
	<div style="text-align: center;">
         <button class="btn btn-success form-control" id="sub" onclick="selectValidity()" style="width: 150px;">提交</button>
     </div>
</div>
<script>
    $(document).ready(
            function () {
                $.jgrid.defaults.styleUI = "Bootstrap";
                var lastSel;
                $("#jqGrid").jqGrid(
                        {
                            url: "${pageContext.request.contextPath}/admin/tusers_authorizeKeysList.action",
                            mtype: 'POST',
                            datatype: "json",
                            width:350,
                            height: 450,
                            autowidth: true,
                            shrinkToFit: true,
//                             rowNum: 10,
//                             rowList: [10, 20, 30],
                            colNames: ['手机', '姓名'],
                            colModel: [ {
                                name: 'TEL',
                                index: 'TEL',
                                editable:true,
                                edittype:"text",
                                editrules:{required: true, number: true,minValue:11}
                            },{
                                name: 'NAME',
                                index: 'NAME',
                                editable:true,
                                edittype:"text",
                                editrules:{required: true}
                            }],
                            sortname: 'sid',
                            rownumbers: true,
                            jsonReader: {
                                root: 'rows',
                                page: "page",
                                total: 'total',
                                records: "records",
                                repeatitems: false
                            },
                            pager: "#jqGrid_paper",
                            viewrecords: false,
                            multiselect: false,
                            hidegrid: false,
                            pgbuttons: false,
                            pginput:false,
                            //双击改变值
                            ondblClickRow: function(id){
//                                 //取当前行的值
//                                 var rowData = $("#jqGrid").jqGrid("getRowData", id); 
//                                 if(id && id!==lastSel){ 
//                                    jQuery('#jqGrid').restoreRow(lastSel); 
//                                    lastSel=id;
//                                 }
//                                 //将当前行edit
//                                 jQuery('#jqGrid').editRow(id, true);
                                $("#jqGrid_iledit").click();
                              }
                        });
			 $("#jqGrid").jqGrid('inlineNav', '#jqGrid_paper', {
			  edittext: "修改",
			  addtext: "新增",
			  savetext: "保存",
			  canceltext: "取消",
			  deltext:"s删",
			  addParams: {
				  position: "afterSelected",
				  addRowParams: {
				   keys: true,
				   oneditfunc: function (rowid) {
// 				    alert("new row with rowid=" + rowid + " are added.");
				   }
				  }
				 },
				editParams: {
				  key: true,
				  oneditfunc: function (rowid) {
// 				   alert("row with rowid=" + rowid + " is editing.");
				  }
				 }
			 });
                $("#jqGrid2").jqGrid(
                        {
                            url: "${pageContext.request.contextPath}/admin/tlocks_tlockList.action?type=private&communityId=${communityId}",
                            mtype: 'POST',
                            datatype: "json",
                            height:285,
                            rowNum: -1,
                            autowidth: true,
                            shrinkToFit: true,
                            colNames: ['序号', '门禁名称', '类型'],
                            colModel: [{
                                name: "SID",
                                index: "SID",
                                key: true,
                                hidden: true
                            }, {
                                name: 'INSTALLLOCKNAME',
                                index: 'INSTALLLOCKNAME'
                            }, {
                                name: '',
                                index: '',
                                formatter: function () {
                                    return '单元门禁';
                                }
                            }],
                            sortname: 'sid',
                            rownumbers: true,
                            jsonReader: {
                                root: 'rows',
                                page: "page",
                                total: 'total',
                                records: "records",
                                repeatitems: false
                            },
//                             pager: "#jqGrid2_paper",
                            viewrecords: false,
                            multiselect: true,
                            hidegrid: false,
                            loadComplete: function (data) {
                            }
                        });
                $("#jqGrid3").jqGrid(
                        {
                            url: "${pageContext.request.contextPath}/admin/tlocks_tlockList.action?type=share&communityId=${communityId}",
                            mtype: 'POST',
                            datatype: "json",
                            height:100,
                            autowidth: true,
                            shrinkToFit: true,
                            rowNum: -1,
                            colNames: ['序号', '门禁名称', '类型'],
                            colModel: [{
                                name: "SID",
                                index: "SID",
                                key: true,
                                hidden: true
                            }, {
                                name: 'INSTALLLOCKNAME',
                                index: 'INSTALLLOCKNAME'
                            }, {
                                name: '',
                                index: '',
                                formatter: function () {
                                    return '公共门禁';
                                }
                            }],
                            sortname: 'sid',
                            rownumbers: true,
                            jsonReader: {
                                root: 'rows',
                                page: "page",
                                total: 'total',
                                records: "records",
                                repeatitems: false
                            },
//                             pager: "#jqGrid3_paper",
                            viewrecords: false,
                            multiselect: true,
                            hidegrid: false,
                            loadComplete: function () {
                            	$("#cb_jqGrid3").click();
                            }
                        });
            });

    
    <!--刷新jqGrid表格数据-->
    function refreshGrid() {
        $("#jqGrid").trigger("reloadGrid");
    }
    
   	function selectValidity(){
   		layer.open({
            type: 2,
            title: ['选择钥匙有效期', 'font-weight:bold'],
            shadeClose: true, //点击遮罩关闭层
            scrollbar: false,
            area: ['400px', '350px'],
            content: '${pageContext.request.contextPath}/admin/tkeys_selectKeysValidity.action',
            btn: ['提交', '取消'],
            yes: function (index, layero) {
            	$("#layui-layer-iframe" + index).contents().find("#sure").click();
            	layer.close(index);
            	submit();
            },
            cancel: function (index) {
                layer.close(index);
            }
        });
	}
    function submit(){
    	$("#sub").attr("disabled", "true");
    	var data='';      
    	var rowData = $("#jqGrid").jqGrid("getRowData");
   	    
    	var prvId = jQuery("#jqGrid2").jqGrid('getGridParam', 'selarrrow');
        var pubId = jQuery("#jqGrid3").jqGrid('getGridParam', 'selarrrow');
        if(prvId=='' && pubId==''){alert("未选择门禁！"); $("#sub").removeAttr("disabled");return; }
        var validity = $("#validity").val();
        if(validity==''){alert("未选择钥匙有效期！"); $("#sub").removeAttr("disabled");return; }
        var bool = 0;
   	    for (var i = 0; i < rowData.length; i++) {  
   	    	if(!rowData[i].TEL){
   	    		continue;
   	    	}
 	   	    if(rowData[i].TEL.indexOf("input")>0){
 	   	    	bool = 1;
 	   	   	    break;
 	   	   	}
 	   		if(!(/^1[3|4|5|7|8]\d{9}$/.test(rowData[i].TEL))){
 	   			bool = 2;
 	   		 	break;
 	 	   	}
   	    	if(data != ''){
   	    		data +=",";
   	    	}
   	    	data += rowData[i].TEL+";"+rowData[i].NAME;
   	    } 
   	    if(bool == 1){
   	   	    alert("列表未保存用户信息，请保存以后再提交！");
   	   		$("#sub").removeAttr("disabled");
   	   	    return;
   	   	}else if(bool == 2){
	   	    alert("用户手机号格式不正确！");
	   	 	$("#sub").removeAttr("disabled");
	   	    return;
	   	}
   	 	if (data=="") { 
   	   	 	alert("所选人员为空！");
   	   		$("#sub").removeAttr("disabled"); 
   	   	 	return; 
   	   	} 
       	$.post("${pageContext.request.contextPath}/admin/tusers_authorizeKeys.action?prvId="+prvId+"&pubId="+pubId, {
   	   		'data':data,
   	   		'validity':validity,
   	   		'communityId':'${communityId}',
   	   		'agtCode':'${agtCode}'
            }, function (result) {
                result = JSON.parse(result)
                if (result.code == 1) {
               	 	layer.alert(result.msg);
               		refreshGrid();
                }else{
                	layer.alert(result.msg);
                }
                $("#sub").removeAttr("disabled");
            });
    }
    function batch(){
    	document.getElementById("upload").click();
    }
    
    $("#upload").change(function () {
        var filename = $(this).val();
        var index = filename.lastIndexOf("\\");
        $("input[name='agtAttr']").val(filename.substring(index + 1));
        jQuery("#jqGrid").jqGrid("clearGridData");
		var formData = new FormData($( "#uploadForm" )[0]); 
        $.ajax({  
            url: '${pageContext.request.contextPath}/admin/tusers_batchUser.action' ,  
            type: 'POST',  
            data: formData,  
            async: false,  
            cache: false,  
            contentType: false,  
            processData: false,  
            success: function (result) {  
                 result = JSON.parse(result);
                 if(result.code == 0){
                	 layer.alert(result.msg);
                 }else{
	                 for(var i=0; i<result.length; i++)  //循环给每行添加数据
	        		{
	        			$("#jqGrid").jqGrid('addRowData',i+1,result[i]);
	        		}; 
                	
                 }
            },   
            error: function (returndata) {  
                alert(returndata);  
            }  
       }); 
//         $("#upload").remove();
//         $("#agtAttr").remove();
//         var input = " <input  id=\"upload\" type=\"file\" name=\"upload\" style=\"display:none\"/><input name=\"agtAttr\"  id=\"agtAttr\" type=\"hidden\"/>";
//         $("#uploadForm").append(input);
  	});
</script>
</body>
</html>
